package com.zmn.plat.admin.controller.notice;

import com.alibaba.fastjson.JSONObject;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dto.ResultDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.InitialUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.model.entity.notice.Notice;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 公告管理
 *
 * @author linfeng
 *
 * @since 2020/12/07 10:44
 */
@RestController
@RequestMapping("/notice/")
public class NoticeModifyController extends NoticeBaseController {

    /**
     * 新增修改
     * 
     * @author linfeng
     * @param notice
     * @since 2020/12/15 11:01
     */
    @RequestMapping(value = "addEdit")
    @RequiresPermissions("/notice/detail.action")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody Notice notice) {
        try {
            noticeBService.addOrModifyNotice(notice);
            return ResponseDTO.success(StatusConsts.STATUS_SUCCESS, "操作成功！");
        } catch (org.springframework.dao.DuplicateKeyException ex) {
            logger.error("同一部门只可配置1条公告！", ex);
            return ResponseDTO.fail("同一部门只可配置1条公告！");
        } catch (Exception ex) {
            logger.error("[zmn]新增/修改公告管理失败,notice：{}", notice, ex);
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 修改状态
     * 
     * @author linfeng
     * @param ids
     * @param status
     * @since 2020/12/15 11:01
     */
    @RequestMapping(value = "updateStatus")
    @RequiresPermissions("/notice/updateStatus.action")
    public ResponseDTO updateStatus(HttpServletRequest request, @RequestParam(value = "ids", defaultValue = "") String ids, @RequestParam(value = "status", defaultValue = "0") Integer status) {
        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的数据！");
        }
        if (NumberUtil.isNullOrZero(status)) {
            return ResponseDTO.fail("请设置状态！");
        }

        List<Integer> arrayIds = StringUtil.convertStrsToList(ids);
        noticeService.updateStatusBatchByKey(arrayIds, status, getMcStaffName(request));

        return ResponseDTO.success(StatusConsts.STATUS_SUCCESS, GlobalConsts.SUCCESS_MSG);
    }

    /**
     * 删除
     *
     * @param id
     * @author linfeng
     * @since 2020/12/08 14:46
     */
    @RequestMapping(value = "delete")
    @RequiresPermissions("/notice/delete.action")
    public ResponseDTO delete(@RequestParam(value = "id", defaultValue = "0") Integer id) {

        if (id == 0) {
            return ResponseDTO.fail("请选择要删除的数据！");
        }

        noticeService.deleteByKey(id);

        return ResponseDTO.success(true, "操作成功！");
    }
}
